<html>
<head><meta charset="utf-8"><title>memory clobber equivalent · project-inline-asm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/index.html">project-inline-asm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html">memory clobber equivalent</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212665924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212665924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212665924">(Oct 08 2020 at 09:31)</a>:</h4>
<p>i have some code using <code>llvm_asm!("" : : : "memory" : "volatile")</code> as a sort of optimization barrier in some code (the case cannot feasibly be made to use black_box or similar), is there an equivalent with new inline asm?</p>
<p>That is to say, I don't see a "memory" clobber-alike. What's my best alternative? someone suggested <code>compiler_fence(SeqCst)</code>, and it works for a few cases, but in general it seems not to be sufficient (unlike "memory" clobber, the compiler won't treat it as a read of unrelated writes, which is required for it to behave as e.g. a strong enouhg optimization barrier to prevent those writes from being removed)</p>
<p>i guess it's fair to argue that this is deliberate/good because this functionality has nothing to do with assembly at all, but ... yeah.</p>



<a name="212666190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212666190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212666190">(Oct 08 2020 at 09:33)</a>:</h4>
<p>did you try asm! options ? Like nomem or nostack ?</p>



<a name="212666600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212666600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212666600">(Oct 08 2020 at 09:37)</a>:</h4>
<blockquote>
<p>That is to say, I don't see a "memory" clobber-alike.</p>
</blockquote>
<p>It is the default with the new asm syntax:</p>
<p><a href="https://rust-lang.github.io/rfcs/2873-inline-asm.html?highlight=inline,assembly#options">https://rust-lang.github.io/rfcs/2873-inline-asm.html?highlight=inline,assembly#options</a></p>
<blockquote>
<p><code>nomem</code> means that the asm code does not read or write to memory. By default the compiler will assume that inline assembly can read or write any memory address that is accessible to it (e.g. through a pointer passed as an operand, or a global).</p>
</blockquote>



<a name="212666944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212666944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212666944">(Oct 08 2020 at 09:40)</a>:</h4>
<blockquote>
<p>By default the compiler will assume that inline assembly can read or write any memory address that is accessible to it</p>
</blockquote>
<p>yeah, that's different. </p>
<p>LLVM's docs say:</p>
<blockquote>
<p>The one exception is that a clobber string of “~{memory}” indicates that the assembly writes to arbitrary undeclared memory locations – not only the memory pointed to by a declared indirect output.</p>
</blockquote>
<p>(in general "memory" clobber roughly means that it could potentially read and write to anything in ways inscrutable to the compiler)</p>



<a name="212666994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212666994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212666994">(Oct 08 2020 at 09:41)</a>:</h4>
<p>The key point is "arbitrary undeclared memory locations" vs "any memory address that is accessible to it"</p>



<a name="212667356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212667356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212667356">(Oct 08 2020 at 09:45)</a>:</h4>
<p>hm, actually maybe your right. my simple test case seems ot work for <code>unsafe { asm!("") }</code>. that <em>seems</em> pretty surprising given that documentation (but maybe "accessible to it" is doing a lot of work there...)</p>



<a name="212667576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212667576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212667576">(Oct 08 2020 at 09:48)</a>:</h4>
<p>the line elsewhere about:</p>
<blockquote>
<p>The set of memory locations that assembly code is allowed the read and write are the same as those allowed for an FFI function. </p>
</blockquote>
<p>also made me think it was intentional that it might be an intentional omission</p>



<a name="212667894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212667894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212667894">(Oct 08 2020 at 09:51)</a>:</h4>
<p>i guess my worry is that i can't actually rely on this much, and given the way those things are worded it's compliant (for e.g. cranelift or whoever) to assume <code>asm!("")</code> does not read or write to anything</p>



<a name="212667977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212667977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212667977">(Oct 08 2020 at 09:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent/near/212666994">said</a>:</p>
<blockquote>
<p>The key point is "arbitrary undeclared memory locations" vs "any memory address that is accessible to it"</p>
</blockquote>
<p>It is always UB to write arbitrary memory locations. The compiler may have stored things like the a return address there.</p>



<a name="212668308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212668308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212668308">(Oct 08 2020 at 09:56)</a>:</h4>
<p>well, right — i'm not actually writing to them. but i am hoping to tell LLVM that it should treat all memory as clobbered.</p>



<a name="212668477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212668477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212668477">(Oct 08 2020 at 09:59)</a>:</h4>
<p>ah, okay, i hadn't realized that <code>llvm_asm!</code>'s "memory" clobber wouldn't clobber a local variable on the stack in the current function which can't have escaped. hmm. that doesn't really matter for me, but i guess that might be what "accessible to it" means...</p>



<a name="212669115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/memory%20clobber%20equivalent/near/212669115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/memory.20clobber.20equivalent.html#212669115">(Oct 08 2020 at 10:06)</a>:</h4>
<p>Well.. I mean if y'all are certain it's intended to be equivalent. It's surprising that the old one would define the set of clobbered values as </p>
<blockquote>
<p>arbitrary undeclared memory locations</p>
</blockquote>
<p>where as this asm explicitly says it's just</p>
<blockquote>
<p>through a pointer passed as an operand, or a global</p>
</blockquote>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>